package main

import (
	"embed"
	_ "embed"
	"errors"
	"gin01/k8s"
	"io/fs"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"strings"

	"github.com/gin-gonic/gin"
)

//go:embed test.txt
var test string

//go:embed static/index.html
var Html []byte

//go:embed static
var Static embed.FS

type Resource struct {
	fs   embed.FS
	path string
}

func NewResource(path string) *Resource {
	return &Resource{
		fs:   Static,
		path: path,
	}
}

func (r *Resource) Open(name string) (fs.File, error) {
	if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) {
		return nil, errors.New("http: invalid character in file path")
	}
	// log.Printf("name: %s\n", name)
	fullName := filepath.Join("static", r.path, name)
	fullName = strings.ReplaceAll(fullName, "\\", "/")
	file, err := r.fs.Open(fullName)
	// log.Printf("fullname: %s\n", fullName)
	// log.Printf("error: %v\n", err)
	return file, err
}

func IndexHandler(c *gin.Context) {
	c.Writer.WriteString(string(Html))
}

const path = "/var/run/secrets/kubernetes.io/serviceaccount"
const TOKEN = path + "/token"
const CACERT = path + "/ca.crt"

func main() {

	// fs, err := Static.Open("static/js/app.036d4b9b.js")
	// if err != nil {
	// 	panic(err)
	// }

	// c, _ := io.ReadAll(fs)
	// log.Printf("js: %s\n", c)
	// fmt.Printf("test: %s\n", test)
	route := gin.Default()

	route.StaticFS("/js", http.FS(NewResource("js")))
	route.StaticFS("/css", http.FS(NewResource("css")))
	// route.StaticFS("/css", http.FS(js))

	// route.StaticFS("/css", http.Dir("static/css"))
	// route.StaticFS("/js", http.Dir("static/js"))
	// route.StaticFile("/favicon.ico", "./favicon.ico")

	// InitResource(route)

	api := route.Group("api/v1")
	{
		api.GET("/test", func(ctx *gin.Context) {
			data := make(map[string]string)
			data["test"] = test

			ctx.JSON(200, gin.H{
				"data": test,
				"map":  data,
			})
		})
	}

	k8sapi := route.Group("api/k8s")
	{
		k8sapi.GET("token", func(ctx *gin.Context) {
			f, _ := ctx.GetQuery("file")

			if f == "" {
				f = `/var/run/secrets/kubernetes.io/serviceaccount/token`
			}
			log.Printf("query#file: %s\n", f)
			b, err := os.ReadFile(f)
			if err != nil {
				ctx.JSON(500, gin.H{"code": 500, "error": err.Error()})
				return
			}
			ctx.JSON(200, gin.H{"code": 200, "content": string(b)})
		})

		k8sapi.GET("api-info", func(c *gin.Context) {
			result, err := k8s.GetApiInfo()
			if err != nil {
				panic(err)
			}
			c.JSON(200, gin.H{"result": result})
		})

		k8sapi.GET("get-pod", func(c *gin.Context) {
			namespace, _ := c.GetQuery("n")
			if namespace == "" {
				namespace = "dev"
			}
			log.Printf("the query namespace: %s\n", namespace)
			result, err := k8s.GetPod(namespace)
			if err != nil {
				panic(err)
			}

			// json.Unmarshal([]byte(result))

			c.JSON(200, gin.H{"result": result})
		})

		k8sapi.GET("all-namespaces", func(c *gin.Context) {
			result, err := k8s.GetNamespaceList()
			if err != nil {
				panic(err)
			}
			c.JSON(200, gin.H{"result": result})
		})

	}

	route.GET("index.html", IndexHandler)
	route.GET("", IndexHandler)

	route.Run(":8088")
}
